package com.yuyue.springFramework.note02;

import com.yuyue.springFramework.note02.entity.AutowireByConstructor;
import com.yuyue.springFramework.note02.entity.AutowireByName;
import com.yuyue.springFramework.note02.entity.AutowireByType;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 《Spring-2 XML自动装配》文章代码
 *  文章地址：https://mp.weixin.qq.com/s/Z6Z6ijG0AmUL2fIuONrBTA
 */
public class Note2Test {
    @Test
    //通过名称自动装配
    public void fun(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("ApplicationContext02.xml");
        AutowireByName autowireByName = applicationContext.getBean("autowireByName", AutowireByName.class);
        //打印自动装配类
        System.out.println(autowireByName);//未匹配的自动装配不报错
    }

    @Test
    //通过类型自动装配
    public void fun1(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("ApplicationContext02.xml");
        AutowireByType autowireByType = applicationContext.getBean("autowireByType", AutowireByType.class);
        //打印自动装配类
        System.out.println(autowireByType);
    }

    @Test
    //通过构造器自动装配
    public void fun3(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("ApplicationContext02.xml");
        AutowireByConstructor autowireByConstructor = applicationContext.getBean("autowireByConstructor", AutowireByConstructor.class);
        //打印自动装配类
        //spring会寻找参数最多的匹配的构造器
        System.out.println(autowireByConstructor);
    }

    @Test
    //默认自动装配
    public void fun4(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("ApplicationContext02.xml");
        AutowireByName autowireByName = applicationContext.getBean("autowireByName1", AutowireByName.class);
        //打印自动装配类
        System.out.println(autowireByName);//未匹配的自动装配不报错
    }
}
